package cn.su.servlet;
import cn.su.pojo.User;
import cn.su.pojo.bill;
import cn.su.pojo.provider;
import cn.su.service.billlistService;
import cn.su.service.impl.billlistServiceimpl;
import cn.su.service.impl.providerServiceimpl;
import cn.su.service.providerService;
import cn.su.utils.BaseDao;
import org.apache.commons.dbutils.QueryRunner;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
//servlet可以直接通过@WebServlet标签配置URL映射  不再需要从web.xml里面配置servlet
@WebServlet(name = "BillList", value = "/jsp/bill.do")
public class BillList extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");

        billlistService billlistService=new billlistServiceimpl();
        HttpSession session = request.getSession();
        String method = request.getParameter("method");//用来判断执行什么功能

        if (method.equals("query")) {
            //query 查询全部订单
            String queryProductName = request.getParameter("queryProductName");//商品名字
            String queryProviderName = request.getParameter("queryProviderName");//供应商名字
            String queryIsPayment = request.getParameter("queryIsPayment");//是否支付
            //  System.out.println(queryProductName+" "+queryProviderName+" "+queryIsPayment);
            List<bill> bills = null;
            List proNameList = null;

            bills = billlistService.billList();
            request.setAttribute("billList", bills);

//            System.out.println(bills.get(0).getId());

            billlistService billService = new billlistServiceimpl();
            List<provider> providers = billService.providerandbill_List();
            //这里是订单管理供应商选择功能，如果用List的话，下拉列表会有重复
            //解决方法，提取名字，放入String集合中
            proNameList = new ArrayList();
            for (int i = 0; i < providers.size(); i++) {
                String name = providers.get(i).getProName();
                if (!proNameList.contains(name)) {
                    proNameList.add(name);  //如果集合中没有重复供货商则加入
                }
            }
            //这里session存放添加订单里的供货商选择
            providerService providerService = new providerServiceimpl();
            List<provider> providersadd = providerService.provider_List();
            request.getSession().setAttribute("providerListadd", providersadd);

            //注意这里不会为null，queryProductName=""
            if (queryProductName != null && queryProviderName != null && queryIsPayment != null) {//根据条件查询
                bills = billlistService.billSearch(queryProductName, queryProviderName, queryIsPayment);
                request.setAttribute("billList", bills);
                request.setAttribute("queryProductName", queryProductName);
                request.setAttribute("queryIsPayment", queryIsPayment);
                request.setAttribute("queryProviderName", queryProviderName);
            }
            request.getSession().setAttribute("providerList", proNameList);

            request.getRequestDispatcher("billlist.jsp").forward(request,response);
        }
    if (method.equals("add")){  //添加订单
        String billCode = request.getParameter("billCode");
        String productName = request.getParameter("productName");
        String productUnit = request.getParameter("productUnit");
        double productCount = Double.parseDouble(request.getParameter("productCount"));
        double totalPrice = Double.parseDouble(request.getParameter("totalPrice"));
        String providerName =  request.getParameter("providerId");//这个是供应商的名字，还不是id
        int isPayment = Integer.parseInt(request.getParameter("isPayment"));
        User user = (User)request.getSession().getAttribute("user");//获取用户id

        bill bill=new bill(-9999,billCode,productName,"",productUnit,productCount,totalPrice,isPayment,user.getId(),new Date(),-9999,new Date(),-9999,providerName);
          billlistService billlistServiceadd=new billlistServiceimpl();
        int i = billlistServiceadd.bill_add(bill);
       if (i<=0){
            request.setAttribute("error","添加失败");
            request.getRequestDispatcher("error.jsp").forward(request,response);
        }else{
           request.setAttribute("queryProductName", bill.getProductName());
           request.setAttribute("queryIsPayment",bill.getIsPayment() );
           request.setAttribute("queryProviderName", bill.getproName());
           //注意这里queryIsPayment 是要String
           List<bill>   bills = billlistService.billSearch(bill.getProductName(),bill.getproName(),bill.getIsPayment()+"");
           request.setAttribute("billList", bills);
        request.getRequestDispatcher("billlist.jsp").forward(request,response);
           //下面这个不能跳转
//response.sendRedirect("/jsp/bill.do?method=query&queryProductName="+bill.getProductName()+"&queryProviderName="+bill.getproName()+"&queryIsPayment="+bill.getIsPayment());
       }

    }
    if (method.equals("chakan")){
        String id1 = request.getParameter("id");
         int id=Integer.parseInt(id1);
         bill bill = billlistService.bill_chakan(id);
          request.setAttribute("bill",bill);
          request.getRequestDispatcher("billview.jsp").forward(request,response);
    }
   if (method.equals("xiugai")){  //跳转到修改的页面，但是没有修改
       String id1 = request.getParameter("id");

       int id=Integer.parseInt(id1);
       bill bill = billlistService.bill_chakan(id);
       request.setAttribute("bill",bill);
       request.getRequestDispatcher("billmodify.jsp").forward(request,response);
   }
   if (method.equals("modifysave")){  //修改保存功能
       String id1 = request.getParameter("id");
       String billCode = request.getParameter("billCode");
       String productName = request.getParameter("productName");
       String productUnit = request.getParameter("productUnit");
       double productCount = Double.parseDouble(request.getParameter("productCount"));
       double totalPrice = Double.parseDouble(request.getParameter("totalPrice"));
       String providerName =  request.getParameter("providerId");//这个是供应商的名字，还不是id
       int isPayment = Integer.parseInt(request.getParameter("isPayment"));
       User user = (User)request.getSession().getAttribute("user");//获取用户id
       bill bill=new bill(-9999,billCode,productName,"",productUnit,productCount,totalPrice,isPayment,user.getId(),new Date(),-9999,new Date(),-9999,providerName);
       billlistService=new billlistServiceimpl();
       //获取隐藏域中的物品id
       int id = billlistService.bill_update(Integer.parseInt(request.getParameter("id")), bill);
       if (id>0){
           request.getRequestDispatcher("/jsp/bill.do?method=query").forward(request,response);
       }else{
           request.getRequestDispatcher("error.jsp").forward(request,response);
       }
   }



        if (method.equals("delete")){

            String id1 =request.getParameter("id");
            int id=Integer.parseInt(id1);
            int i = billlistService.bill_delete(id);
            if (i>0){//刪除成功
                PrintWriter writer = response.getWriter();
                writer.println("<script>");
                writer.println("alert('删除成功！')");
                writer.println("</script>");
                request.getRequestDispatcher("/jsp/bill.do?method=query").forward(request,response);
            }else{
                PrintWriter writer = response.getWriter();
                writer.println("<script>");
                writer.println("alert('删除失败！');");
                writer.println("</script>");
                response.sendRedirect("error.jsp");
            }
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         this.doGet(request, response);
    }
}
